﻿// TcpipDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "TcpipDlg.h"
#include "afxdialogex.h"


// CTcpipDlg 对话框

IMPLEMENT_DYNAMIC(CTcpipDlg, CDialogEx)

CTcpipDlg::CTcpipDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_TCPIP, pParent)
	, m_nPort(0)
	, m_bCheckAuto(FALSE)
	, m_nPort_2(0)
	, m_nFangdaiPort(0)
	, FangdaiFlag(FALSE)
	, m_nPort_W(0)
	, m_nPort_White(0)
	, GlueErrorFlag(FALSE)
{

}

CTcpipDlg::~CTcpipDlg()
{
}

void CTcpipDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_MODE, m_comboTcpMode);
	DDX_Control(pDX, IDC_IPADDRESS_IP, m_ipAdd);
	DDX_Text(pDX, IDC_EDIT_PORT, m_nPort);
	DDX_Check(pDX, IDC_CHECK_AUTO, m_bCheckAuto);
	DDX_Control(pDX, IDC_IPADDRESS_IP3, m_ipFangdai);
	DDX_Text(pDX, IDC_EDIT_PORT7, m_nFangdaiPort);
	DDX_Check(pDX, IDC_CHECK_AUTO2, FangdaiFlag);
	DDX_Control(pDX, IDC_IPADDRESS_IP4, m_ipAdd_W);
	DDX_Text(pDX, IDC_EDIT_PORT8, m_nPort_W);
	DDX_Control(pDX, IDC_IPADDRESS_IP5, m_ipAdd_White);
	DDX_Text(pDX, IDC_EDIT_PORT9, m_nPort_White);
	DDX_Check(pDX, IDC_CHECK_AUTO3, GlueErrorFlag);
}


BEGIN_MESSAGE_MAP(CTcpipDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CTcpipDlg::OnBnClickedButtonSave)
	ON_BN_CLICKED(IDC_BUTTON_BUILD, &CTcpipDlg::OnBnClickedButtonBuild)
	ON_BN_CLICKED(IDC_BUTTON1, &CTcpipDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON_BUILD3, &CTcpipDlg::OnBnClickedButtonBuild3)
	ON_BN_CLICKED(IDC_BUTTON2, &CTcpipDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CTcpipDlg::OnBnClickedButton3)
END_MESSAGE_MAP()


// CTcpipDlg 消息处理程序


BOOL CTcpipDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	ReadConfigIni();

	m_comboTcpMode.ResetContent();
	m_comboTcpMode.AddString(L"0: 服务器");
	m_comboTcpMode.AddString(L"1: 客户端");
	m_comboTcpMode.SetCurSel(gSystemMgr.m_nModeSelect);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


void CTcpipDlg::OnCancel()
{
	// TODO: 在此添加专用代码和/或调用基类

	CDialogEx::OnCancel();
}


void CTcpipDlg::OnBnClickedButtonSave()
{
	SaveValueToConfig();
}


void CTcpipDlg::OnBnClickedButtonBuild()
{
	if (gSystemMgr.m_nModeSelect == 0) {
		USES_CONVERSION;
		char* c_strIp = T2A(gSystemMgr.m_cstrIPAdd);
		bool bCreateServer = gSystemMgr.m_cSocketServer.OpenSocketServer("Server", c_strIp, gSystemMgr.m_nPort);
		CString cstr;
		cstr.Format(L"Socket Server Build result:%d", bCreateServer);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		if (bCreateServer)
		{
			gSystemMgr.StartSocketAcceptClientThread();
		}
	}
	else
	{
		USES_CONVERSION;
		char* c_strIp = T2A(gSystemMgr.m_cstrIPAdd);
		bool bCreateServer = gSystemMgr.m_cClientGlue.ClientConnectionServer(c_strIp, m_nPort);
		//CString cstr;
		//cstr.Format(L"Socket Client Build result:%d", bCreateServer);
		//gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		//m_cClientWhite.ClientConnectionServer("192.168.234.11", 1004);
		Sleep(100);
	}
}

void CTcpipDlg::ReadConfigIni()
{
	m_ipAdd.SetWindowText(gSystemMgr.m_cstrIPAdd);
	m_nPort = gSystemMgr.m_nPort;
	(gSystemMgr.m_bAutoFlag == 1) ? m_bCheckAuto = true : m_bCheckAuto = false;

	m_ipFangdai.SetWindowText(gSystemMgr.m_cstrIPAdd2);
	m_nFangdaiPort = gSystemMgr.m_nPort2;

	FangdaiFlag = gSystemMgr.FangdaiFlag;

	m_ipAdd_W.SetWindowText(gSystemMgr.m_cstrIPAdd_W);
	m_nPort_W = gSystemMgr.m_nPort_W;

	m_ipAdd_White.SetWindowText(gSystemMgr.m_cstrIPAdd_White);
	m_nPort_White = gSystemMgr.m_nPort_White;

	GlueErrorFlag = gSystemMgr.GlueErrorFlag;

	UpdateData(FALSE);
}

void CTcpipDlg::SaveValueToConfig()
{
	UpdateData(TRUE);

	m_ipAdd.GetWindowText(gSystemMgr.m_cstrIPAdd);
	gSystemMgr.m_nPort = m_nPort;
	(m_bCheckAuto == TRUE) ? gSystemMgr.m_bAutoFlag = 1 : gSystemMgr.m_bAutoFlag = 0;
	gSystemMgr.m_nModeSelect = m_comboTcpMode.GetCurSel();

	m_ipFangdai.GetWindowText(gSystemMgr.m_cstrIPAdd2);
	gSystemMgr.m_nPort2 = m_nFangdaiPort;

	m_ipAdd_W.GetWindowText(gSystemMgr.m_cstrIPAdd_W);
	gSystemMgr.m_nPort_W = m_nPort_W;

	m_ipAdd_White.GetWindowText(gSystemMgr.m_cstrIPAdd_White);
	gSystemMgr.m_nPort_White = m_nPort_White;

	gSystemMgr.FangdaiFlag = FangdaiFlag;

	gSystemMgr.GlueErrorFlag = GlueErrorFlag;

	CString cstr;
	cstr.Format(L"%d", gSystemMgr.GlueErrorFlag);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"GlueErrorFlag", cstr);

	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_cstrIPAdd_W", gSystemMgr.m_cstrIPAdd_W);
	cstr.Format(L"%d", gSystemMgr.m_nPort_W);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_nPort_W", cstr);

	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_cstrIPAdd_White", gSystemMgr.m_cstrIPAdd_White);
	cstr.Format(L"%d", gSystemMgr.m_nPort_White);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_nPort_White", cstr);


	cstr.Format(L"%d", gSystemMgr.FangdaiFlag);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"FangdaiFlag", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nModeSelect);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_nModeSelect", cstr);

	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_cstrIPAdd", gSystemMgr.m_cstrIPAdd);

	cstr.Format(L"%d", gSystemMgr.m_nPort);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_nPort", cstr);

	cstr.Format(L"%d", gSystemMgr.m_bAutoFlag);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_bAutoFlag", cstr);

	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_cstrIPAdd2", gSystemMgr.m_cstrIPAdd2);

	cstr.Format(L"%d", gSystemMgr.m_nPort2);
	gSystemMgr.m_cTcpipConfigIni.SaveConfigIni(L"TcpipConfig", L"m_nPort2", cstr);

	gSystemLog.DisplayLogAndSave(L"Tcpip保存数据完成", LV_EVENT);
}

void CTcpipDlg::OnBnClickedButton1()
{
	CString cstr;
	cstr.Format(L"Trigger");
	gSystemMgr.m_cClientGlue.m_sendBuffer.push_front(cstr.GetBuffer());
}


void CTcpipDlg::OnBnClickedButtonBuild3()
{
	if (gSystemMgr.m_nModeSelect == 0) {
		USES_CONVERSION;
		char* c_strIp = T2A(gSystemMgr.m_cstrIPAdd2);
		bool bCreateServer = gSystemMgr.m_cSocketServer2.OpenSocketServer("Server2", c_strIp, gSystemMgr.m_nPort2);
		CString cstr;
		cstr.Format(L"Socket Server 2 Build result:%d", bCreateServer);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR);
		if (bCreateServer)
		{
			gSystemMgr.StartSocketAcceptClientThread2();
		}
	}
	else
	{
		USES_CONVERSION;
		char* c_strIp = T2A(gSystemMgr.m_cstrIPAdd2);
		bool bCreateServer = gSystemMgr.m_cClientFangdai.ClientConnectionServer(c_strIp, gSystemMgr.m_nPort2);
		Sleep(100);
	}
}


void CTcpipDlg::OnBnClickedButton2()
{
	CString cstr;
	cstr.Format(L"START");//_test
	gSystemMgr.m_cClientFangdai.m_sendBuffer.push_front(cstr.GetBuffer());
}


void CTcpipDlg::OnBnClickedButton3()
{
	CString cstr;
	cstr.Format(L"WHITETEST;TestSN");
	gSystemMgr.m_cClientWhite.m_sendBuffer.push_front(cstr.GetBuffer());
}
